/*
    BibleNotebook - A bible reader software
    Copyright (C) 2014  Williams Fernando Lopez

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
 */

package com.simplebible.bible;

import wl.util.Utils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.simplebible.bible.objects.Abreviation;
import com.simplebible.bible.objects.Bible;
import com.simplebible.bible.objects.Book;
import com.simplebible.bible.objects.Chapter;
import com.simplebible.bible.objects.SearchResult;
import wl.util.TextPosition;
import com.simplebible.bible.objects.Verse;

/**
 *
 * @author Williams Lopez
 */
public class BibleUtils {


    public static final int SEARCH_LIMIT = 3000;
    

    

    public static Book findBook(Bible bible,String abrv){
        List<Book> books= bible.getBooks();

        for(Book b : books){
            if(abrv.equalsIgnoreCase(b.getAbreviation().getBook())){
                return b;
            }
        }

        return null;
    }

    public static Chapter findChapter(Book book, String abrv){
        List<Chapter> chapters= book.getChapters();

        for(Chapter c : chapters){
            if(abrv.equalsIgnoreCase(c.getAbreviation().getChapter())){
                return c;
            }
        }
        return null;
    }

    public static Verse findVerse(Chapter chapter,String abrv){
        List<Verse> verses = chapter.getVerses();
        for (Verse v : verses) {
            if(abrv.equalsIgnoreCase(v.getAbreviation().getVerse())){
                return v;
            }
            List<Abreviation> adicional = v.getAbreviation().getAditionalReferences();
            if(adicional!=null && !adicional.isEmpty()){
                for (Abreviation ad : adicional) {
                    if (abrv.equalsIgnoreCase(ad.getVerse())) {
                        return v;
                    }
                }
            }

        }
        return null;
    }


    public static Verse findVerse(Bible bib, Abreviation abrv){
        Book b = findBook(bib, abrv.getBook());
        Chapter ch = findChapter(b, abrv.getChapter());
        Verse v = findVerse(ch, abrv.getVerse());
        return v;
    }


    


    public static List<SearchResult> foundInBible(Bible bible,String searchText){
        boolean matchCase = false;
        boolean completeWord = true;
        boolean ignorarTildes = true;
        int count = 0;

        List<SearchResult> result = new ArrayList<SearchResult>();
        for(Book b : bible.getBooks()){
            for(Chapter c : b.getChapters()){
                for(Verse v : c.getVerses()){
                    List<TextPosition> foundsInVerse = Utils.searchInText(v.getTextWithoutMarks(), searchText, matchCase, completeWord, ignorarTildes);
                    if(foundsInVerse != null && !foundsInVerse.isEmpty()){
                        result.add(new SearchResult(v, foundsInVerse));
                        count++;
                        if(count >= SEARCH_LIMIT){
                            return result;
                        }
                    }
                }
            }
        }
        return result;
    }


    public static String getHumanName(Bible bible,Abreviation abrv){
        return getHumanName(bible, abrv, 0);
    }

    public static String getHumanName(Bible bible,Abreviation abrv, int truncateBookName){

        if(bible == null){
            return abrv.toString();
        }

        Book book = findBook(bible, abrv.getBook());
        if(book==null){
            return null;
        }
        Chapter ch =  findChapter(book, abrv.getChapter());

        if(ch==null){
            return book.getHumanName();
        }

        Verse vr  = findVerse(ch, abrv.getVerse());

        if(vr==null){
            return book.getHumanName()+" "+ch.getHumanName();
        }

        return Utils.truncateString(book.getHumanName(), truncateBookName)+" "+ch.getHumanName() +":"+ vr.getHumanName();
    }


    


    public static Abreviation getHumanDataFromText(String text){

        Abreviation abrv = new Abreviation();

        String[] splited = text.split("[ ]");
        if(splited.length > 1){
            String datos[] =  splited[splited.length -1].split("[:]");
            if(datos.length>0 && Utils.isInteger(datos[0].trim())){
                abrv.setChapter(datos[0].trim());
            }
            if(datos.length>1 && Utils.isInteger(datos[1].trim())){
                abrv.setVerse(datos[1].trim());
            }
            text = "";
            for(int i=0;i<splited.length-1;i++){
                text += splited[i];
            }
            abrv.setBook(text);
        }else{
            abrv.setBook(text);
        }

        return abrv;
    }

    public static boolean removeBible(String id) throws Exception {
        BibleReader.cleanBiblesCache();
        Thread.currentThread().sleep(500);
        File file1 = new File(BibleReader.root + File.separator + id);
        File file2 = new File(BibleReader.root + File.separator + id + ".files");
        return (!file1.delete() || !file2.delete());
    }


   
   

}
